package com.xm.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 数据源连接池
 */
public class DataSourceUtil {
    private DataSourceUtil() {}

    private static final String driverClass = "com.mysql.cj.jdbc.Driver";
    private static final String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/huakaiyoushi?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&allowMultiQueries=true&rewriteBatchedStatements=true";
    private static final String username = "root";
    protected static final String password = "123456";

    /**
     * 单批次提交最大计数
     */
    @Getter
    private static final int commitSize = 1000;
    public static AtomicLong atomicLong = new AtomicLong(0);

    private static final HikariDataSource hikariDataSource;
    static {
        hikariDataSource = new HikariDataSource();
        hikariDataSource.setDriverClassName(driverClass);
        hikariDataSource.setJdbcUrl(jdbcUrl);
        hikariDataSource.setUsername(username);
        hikariDataSource.setPassword(password);
        hikariDataSource.setMaximumPoolSize(20);//最大连接数
        hikariDataSource.setMinimumIdle(5);//最小空闲连接数
        hikariDataSource.setIdleTimeout(30000L);//空闲连接超时时间，30s
        hikariDataSource.setMaxLifetime(1800000L);//连接最大存活时间，30min
        hikariDataSource.setConnectionTimeout(30000L);//连接超时时间，30s
    }
    public static DataSource getHikariDataSource() {
        return hikariDataSource;
    }
    public static Connection getHikariConnection() throws SQLException {
        return hikariDataSource.getConnection();
    }

    private static final ComboPooledDataSource c3p0DataSource;
    static {
        try {
            c3p0DataSource = new ComboPooledDataSource();
            c3p0DataSource.setDriverClass(driverClass);
            c3p0DataSource.setJdbcUrl(jdbcUrl);
            c3p0DataSource.setUser(username);
            c3p0DataSource.setPassword(password);
            c3p0DataSource.setMaxPoolSize(20);//连接池中允许的最大连接数
            c3p0DataSource.setMinPoolSize(5);//连接池中保持的最小连接数
            c3p0DataSource.setInitialPoolSize(10);//连接池初始化时创建的连接数
            c3p0DataSource.setAcquireIncrement(5);//当连接池中的连接耗尽时，一次性创建的新连接数
            c3p0DataSource.setMaxIdleTime(300);//连接的最大空闲时间，超过该时间未使用的连接会被释放，5min
            c3p0DataSource.setMaxStatements(50);//连接池缓存的最大 PreparedStatement 数量
            c3p0DataSource.setIdleConnectionTestPeriod(60);//检查空闲连接的时间间隔（单位：秒）
            c3p0DataSource.setTestConnectionOnCheckout(true);//从连接池获取连接时是否测试连接的有效性
        } catch (PropertyVetoException e) {
            throw new RuntimeException(e);
        }
    }
    public static DataSource getC3p0DataSource() {
        return c3p0DataSource;
    }
    public static Connection getC3p0Connection() throws SQLException {
        return c3p0DataSource.getConnection();
    }


}
